package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.DebugAdapter;
import flc.ast.bean.MyDebugBean;
import flc.ast.databinding.ActivityDebugBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.view.container.StkRelativeLayout;

public class DebugActivity extends BaseAc<ActivityDebugBinding> {

    public static int type;
    private DebugAdapter debugAdapter;
    private List<MyDebugBean> listShow = new ArrayList<>();
    private Dialog myDeleteDialog;
    private int selPosition;


    @Override
    protected int onCreate() {
        return R.layout.activity_debug;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDebugBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDebugAdd.setOnClickListener(this);

        mDataBinding.rvDebugList.setLayoutManager(new LinearLayoutManager(mContext));
        mDataBinding.rvDebugList.setItemAnimator(new DefaultItemAnimator());
        debugAdapter = new DebugAdapter(this);
        mDataBinding.rvDebugList.setAdapter(debugAdapter);
        debugAdapter.setOnItemClickListener(new DebugAdapter.OnItemClickListener() {
            @Override
            public void onClick(MyDebugBean bean, int position) {

            }

            @Override
            public void onClickDelete(MyDebugBean bean, int position) {
                myDeleteDialog.show();
                selPosition = position;
            }

            @Override
            public void onClickEdit(MyDebugBean bean, int position) {
                AddDebugActivity.type = type;
                AddDebugActivity.isEdit = true;
                AddDebugActivity.selPosition = position;
                startActivity(AddDebugActivity.class);
            }
        });

        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDebugAdd://添加
                AddDebugActivity.type = type;
                AddDebugActivity.isEdit = false;
                startActivity(AddDebugActivity.class);
                break;
            //删除弹框
            case R.id.tvDialogDeleteConfirm:
                delete();
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (type == 0) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.tcp_server));
            getTcpServerData();
        } else if (type == 1) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.tcp_client));
            getTcpClientData();
        } else if (type == 2) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.udp_server));
            getUdpServerData();
        } else if (type == 3) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.udp_client));
            getUdpClientData();
        }
    }

    //删除
    private void delete() {
        listShow.remove(selPosition);
        if (type == 0) {
            AppSpUtils.saveTcpServerList(listShow);
            getTcpServerData();
        } else if (type == 1) {
            AppSpUtils.saveTcpClientList(listShow);
            getTcpClientData();
        } else if (type == 2) {
            AppSpUtils.saveUdpServerList(listShow);
            getUdpServerData();
        } else if (type == 3) {
            AppSpUtils.saveUdpClientList(listShow);
            getUdpClientData();
        }
        ToastUtils.showShort(getString(R.string.delete_suc));
    }

    private void getTcpServerData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getTcpServerList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setmDatas(listShow);
            debugAdapter.notifyDataSetChanged();
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.refreshDrawableState();
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.tvDebugNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.tvDebugNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getTcpClientData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getTcpClientList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setmDatas(listShow);
            debugAdapter.notifyDataSetChanged();
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.refreshDrawableState();
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.tvDebugNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.tvDebugNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getUdpServerData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getUdpServerList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setmDatas(listShow);
            debugAdapter.notifyDataSetChanged();
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.refreshDrawableState();
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.tvDebugNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.tvDebugNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getUdpClientData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getUdpClientList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setmDatas(listShow);
            debugAdapter.notifyDataSetChanged();
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.refreshDrawableState();
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.tvDebugNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.tvDebugNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogDeleteConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogDeleteCancel);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

}